package com.regan.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.regan.erp.aop.NotNullPara;
import com.regan.erp.controller.base.BaseController;
import com.regan.erp.model.Dict;
import com.regan.erp.model.Menu;
import com.regan.erp.model.ResultPoJo;
import com.regan.erp.service.DictService;
import com.regan.erp.util.Constants;

import java.util.List;

/**
 * 字典管理
 * @action /dict
 *
 * @author zhangby
 * @date 2018/10/15 下午5:19
 */
public class DictController extends BaseController {
    /** 字典服务 */
    DictService dictService = Duang.duang(DictService.class);

    /**
     * 获取字典列表数据
     * @title 字典列表查询
     *
     * @resqParam id|'id'| int|必填
     * @resqParam parent_id|'父类id'| int|非必填
     * @resqParam value|'值'|String|非必填
     * @resqParam label|'标签'|String|非必填
     * @resqParam type|'类型: 0正常，1停用'| char|非必填
     * @resqParam sort|'排序'| int|非必填
     * @resqParam create_date|'创建时间'|date|非必填
     * @resqParam update_date|'更新时间'|date|非必填
     * @resqParam create_user_id|'创建用户'|String|非必填
     * @resqParam update_user_id|'更新用户'|String|非必填
     * @resqParam remarks|'备注'|String|非必填
     * @resqParam del_flag|'删除标记  0：正常，1：删除'| char|非必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/15 下午5:21
     */
    public void getDictListData() {
        renderParseJson(ResultPoJo.create().result(dictService.getDictListData(getParamDict(true))));
    }

    /**
     *
     * 保存字典数据
     * @title 字典保存
     * @param id|'id'| int|必填
     * @param parent_id|'父类id'| int|非必填
     * @param value|'值'|String|非必填
     * @param label|'标签'|String|非必填
     * @param type|'类型: 0正常，1停用'| char|非必填
     * @param sort|'排序'| int|非必填
     * @resqParam code|状态码|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/15 下午5:36
     */
    @NotNullPara({"value","label"})
    @Before(Tx.class)
    public void saveDict() {
        Dict dict = getModel(Dict.class, "", true);
        //设置默认值
        dict.setParentId(Integer.parseInt(getPara("parent_id", "0")));
        dict.setType(getPara("type", "0"));
        dict.setSort(Integer.parseInt(getPara("sort", "10")));
        save(dict);
        renderJson(ResultPoJo.create());
    }

    /**
     * 获取字典值
     * @title 获取字典信息
     * @param  id|'id'| int|必填
     * @resqParam id|'id'| int|必填
     * @resqParam parent_id|'父类id'| int|非必填
     * @resqParam value|'值'|String|非必填
     * @resqParam label|'标签'|String|非必填
     * @resqParam type|'类型: 0正常，1停用'| char|非必填
     * @resqParam sort|'排序'| int|非必填
     * @resqParam create_date|'创建时间'|date|非必填
     * @resqParam update_date|'更新时间'|date|非必填
     * @resqParam create_user_id|'创建用户'|String|非必填
     * @resqParam update_user_id|'更新用户'|String|非必填
     * @resqParam remarks|'备注'|String|非必填
     * @resqParam del_flag|'删除标记  0：正常，1：删除'| char|非必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/16 上午11:49
     */
    @NotNullPara("id")
    public void getDict() {
        Dict dict = Dict.dao.findById(getPara("id"));
        renderParseJson(ResultPoJo.create().result(dict));
    }

    /**
     *
     * 删除字典
     * @title 删除字典
     * @param  ids|多个'id'，用逗号间隔| int|必填
     * @resqParam code|状态码|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/16 上午11:57
     */
    @NotNullPara("ids")
    public void delteDict() {
        String[] ids = getPara("ids", "").split(",");
        for (String id : ids) {
            new Dict().setId(Integer.parseInt(id)).setDelFlag(Constants.YES_NO_TYPE_1).update();
        }
        renderJson(ResultPoJo.create());
    }

    /**
     *
     * 获取字典最大排序
     * @title 获取最大排序码
     * @param  parent_id|父级菜单id|int|必填
     * @resqParam code|状态码|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/15 上午10:31
     */
    @NotNullPara("parent_id")
    public void getDictMaxSort() {
        Record record = Db.findFirst("select sort sort from t_dict where del_flag=0 and parent_id = ? ORDER BY sort desc LIMIT 1", getPara("parent_id"));
        Integer sort = ObjectUtil.isNull(record)?10:(record.getInt("sort")/10)*10+10;
        //默认排序，为最高排序加10
        renderJson(ResultPoJo.create().result(Kv.create().set("sort",sort)));
    }

    /**
     * 获取查询下拉数据
     * @title 获取查询条件下拉数据
     * @param parent_id|父级菜单id|int|必填
     * @resqParam item|下拉数据|array|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/10/17 上午11:16
     */
    @NotNullPara("parent_id")
    public void getQueryItem() {
        List<String> item = Lists.newArrayList();
        List<Record> records = Db.find("select label,value from t_dict where parent_id = ? ORDER BY sort ", getPara("parent_id"));
        records.forEach(record -> {
            item.add(record.getStr("label"));
            if (ObjectUtil.equal(getPara("parent_id"), "0")) {
                item.add(record.getStr("value"));
            }
        });
        renderJson(ResultPoJo.create().result(item));
    }
}
